import Dexie, { Table } from 'dexie';
import { mock } from 'mockjs';

import { Status } from './types/status.d';
import { Menu, MenuType } from './types/menu.d';
import { Role, RoleSystem } from './types/role.d';
import { Token } from './types/token.d';
import { User } from './types/user.d';
import { Dict } from './types/dict.d';

export class MySubClassedDexie extends Dexie {
  users!: Table<User>;
  roles!: Table<Role>;
  menus!: Table<Menu>;
  tokens!: Table<Token>;
  dicts!: Table<Dict>;

  constructor() {
    super('myDatabase');
    this.version(1).stores({
      // 定义表的主键与索引，索引可以提高检索性能。
      users: '++uid, &name, status, [name+password]',
      roles: 'key, status, *menus, [key+status]',
      menus: 'key, status, parent, [parent+key]',
      tokens: '++id, token, uid, status',
      dicts: 'key, parent, status, [parent+key]',
    });

    this.version(2)
      .stores({})
      .upgrade(async (tx) => {
        console.log(
          '🚀 ~ file: index.ts:30 ~ MySubClassedDexie ~ this.version ~ tx:',
          tx,
        );
        await tx
          .table('menus')
          .toCollection()
          .modify((menu) => {
            menu.createTime = menu.creationTime;
            delete menu.creationTime;
          });
      });
  }
}
const db = new MySubClassedDexie();
db.on('populate', async () => {
  const createTime = new Date();

  // console.log(
  //   mock({
  //     'list|100': [
  //       {
  //         uid: '@natural(6,1000)',
  //         name: '@word(6,20)',
  //         password: '123456',
  //         roles: ['guest'],
  //         status: Status.enable,
  //         createTime,
  //       },
  //     ],
  //   }),
  // );
  // 并发
  Promise.all([
    db.users.bulkAdd([
      {
        uid: 1,
        name: 'admin',
        password: 'admin',
        roles: ['admin'],
        status: Status.enable,
        createTime,
      },
      {
        uid: 2,
        name: 'editor',
        password: '123456',
        roles: ['editor'],
        status: Status.enable,
        createTime,
      },
      {
        uid: 3,
        name: 'customerService',
        password: '123456',
        roles: ['customerService'],
        status: Status.enable,
        createTime,
      },
      {
        uid: 4,
        name: 'user003',
        password: '123456',
        roles: ['editor', 'customerService'],
        status: Status.enable,
        createTime,
      },
      {
        uid: 5,
        name: 'user004',
        password: '123456',
        roles: ['guest'],
        status: Status.enable,
        createTime,
      },
      ...mock({
        'list|100': [
          {
            // uid: '@natural(6,1000)',
            name: '@word(6,12)',
            password: '123456',
            roles: ['guest'],
            'status|1': [Status.enable, Status.disable],
            createTime,
          },
        ],
      }).list,
    ]),
    db.roles.bulkAdd([
      {
        key: 'admin',
        title: '管理员',
        menus: [],
        system: RoleSystem.yes,
        status: Status.enable,
        createTime,
      },
      {
        key: 'editor',
        title: '编辑',
        menus: ['system', 'system-user', 'system-user-uid'],
        system: RoleSystem.no,
        status: Status.enable,
        createTime,
      },
      {
        key: 'customerService',
        title: '客服',
        menus: ['system', 'system-role', 'system-role-key'],
        system: RoleSystem.no,
        status: Status.enable,
        createTime,
      },
      {
        key: 'guest',
        title: '游客',
        menus: [],
        system: RoleSystem.no,
        status: Status.enable,
        createTime,
      },
    ]),
    db.menus.bulkAdd([
      {
        parent: '',
        key: 'system',
        title: '系统管理',
        path: '',
        icon: 'eos-icons:system-group',
        type: MenuType.subMenu,
        status: Status.enable,
        createTime,
        sort: 1,
      },
      {
        parent: 'system',
        key: 'system-user',
        title: '用户管理',
        path: '/system/user',
        icon: '',
        type: MenuType.page,
        status: Status.enable,
        createTime,
        sort: 1,
      },
      {
        parent: 'system-user',
        key: 'system-user-uid',
        title: '用户详情',
        path: '/system/user/:uid',
        icon: '',
        type: MenuType.page,
        status: Status.enable,
        createTime,
        sort: 1,
      },
      {
        parent: 'system',
        key: 'system-role',
        title: '角色管理',
        path: '/system/role',
        icon: '',
        type: MenuType.page,
        status: Status.enable,
        createTime,
        sort: 2,
      },
      {
        parent: 'system-role',
        key: 'system-role-key',
        title: '角色详情',
        path: '/system/role/:key',
        icon: '',
        type: MenuType.page,
        status: Status.enable,
        createTime,
        sort: 1,
      },
      {
        parent: 'system',
        key: 'system-menu',
        title: '菜单管理',
        path: '/system/menu',
        icon: '',
        type: MenuType.page,
        status: Status.enable,
        createTime,
        sort: 3,
      },
      {
        parent: 'system-menu',
        key: 'system-menu-key',
        title: '菜单详情',
        path: '/system/menu/:key',
        icon: '',
        type: MenuType.page,
        status: Status.enable,
        createTime,
        sort: 1,
      },
      {
        parent: 'system-menu',
        key: 'system-menu-add',
        title: '新建菜单',
        path: '/system/menu/add',
        icon: '',
        type: MenuType.page,
        status: Status.enable,
        createTime,
        sort: 1,
      },
      {
        parent: 'system',
        key: 'system-dept',
        title: '部门管理',
        path: '/system/dept',
        icon: '',
        type: MenuType.page,
        status: Status.disable,
        createTime,
        sort: 4,
      },
      {
        parent: 'system-dept',
        key: 'system-dept-key',
        title: '部门详情',
        path: '/system/dept/:key',
        icon: '',
        type: MenuType.page,
        status: Status.disable,
        createTime,
        sort: 1,
      },
    ]),
    db.dicts.bulkAdd([
      {
        key: 'system.status',
        parent: '',
        title: '状态',
        status: Status.enable,
        createTime,
        sort: 1,
      },
      {
        key: 'system.status.enable',
        parent: 'system.status',
        title: '启用',
        status: Status.enable,
        createTime,
        sort: 1,
        value: Status.enable,
      },
      {
        key: 'system.status.disable',
        parent: 'system.status',
        title: '禁用',
        status: Status.enable,
        createTime,
        sort: 2,
        value: Status.disable,
      },
      {
        key: 'system.menuType',
        parent: '',
        title: '菜单类型',
        status: Status.enable,
        createTime,
        sort: 1,
      },
      {
        key: 'system.menuType.subMenu',
        parent: 'system.menuType',
        title: '目录',
        status: Status.enable,
        createTime,
        sort: 1,
        value: MenuType.subMenu,
      },
      {
        key: 'system.menuType.group',
        parent: 'system.menuType',
        title: '分组',
        status: Status.enable,
        createTime,
        sort: 2,
        value: MenuType.group,
      },
      {
        key: 'system.menuType.page',
        parent: 'system.menuType',
        title: '页面',
        status: Status.enable,
        createTime,
        sort: 3,
        value: MenuType.page,
      },
    ]),
  ]);
});

(async () => {
  // const data = await db.menus.get({ parent: '' });
  // console.log('🚀 ~ file: index.ts:239 ~ data:', data);
})();

export default db;
